package com.burgeon.backend.SHOPBACKEND.rest.marketing;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.MarketingItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.*;

public class QueryMarketingItemList extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QueryMarketingItemList(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList getData(String[] id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<MarketingItem> marketingArrayList = new ArrayList<MarketingItem>();
        for (int i=0;i<jsonArray.length();i++){
            JSONArray array = jsonArray.getJSONArray(i);
            MarketingItem marketingItem = new MarketingItem();

            marketingItem.setId(array.getString(0));
            marketingItem.setNum(array.getString(1));
            marketingItem.setDescriptio(array.getString(2));
            marketingItem.setName(array.getString(3));
            marketingItem.setMarkentingId(array.getString(4));
            marketingItem.setNO(array.getString(5));
            marketingArrayList.add(marketingItem);
        }


        Set set = new HashSet();
        for (MarketingItem marketingItem : marketingArrayList){
            set.add(marketingItem.getName()+","+marketingItem.getMarkentingId());
        }
        ArrayList<String> list_1 = new ArrayList<>(set);
        ArrayList data = new ArrayList();
        for (String o : list_1) {
            HashMap bean = new HashMap();
            ArrayList item = new ArrayList();
            for (MarketingItem marketingItem : marketingArrayList) {
                if (o.split(",")[0].equals(marketingItem.getName())) {
                    HashMap hashMap = new HashMap();
                    hashMap.put("id",marketingItem.getId());
                    hashMap.put("name",marketingItem.getDescriptio());
                    hashMap.put("num",marketingItem.getNum());
                    hashMap.put("NO",marketingItem.getNO());
                    item.add(hashMap);
                }
            }
            bean.put("name", o.split(",")[0]);
            bean.put("id",o.split(",")[1]);

            bean.put("item",item);
            data.add(bean);
        }


        return data;
    }

    private MultiValueMap Data(String[] arr) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("VIP_QTY");//
        columns.add("DESCRIPTION");//
        columns.add("'YC_LABELDEFINE_ID;NAME'");
        columns.add("YC_LABELDEFINE_ID");
        columns.add("'YC_LABELDEFINE_ID;NO'");


        params.put("columns", columns);
        params.put("table", "YC_LABELDEFINEITEM");//表名

        params.put("count","true");


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column", "'YC_LABELDEFINE_ID'");
        expr1.put("condition", "\" IN ("+ toString(arr)+")\"");

        expr2.put("column", "ISENABLE");
        expr2.put("condition", "Y");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }
    private String toString(String[] data){

        return Arrays.toString(data).replace("\"","'")
                .replace("[","")
                .replace("]","")
                .replace(" ", "");

    }
    public static void main(String[] args) {
        String[] arr = new String[]{"2"};
        QueryMarketingItemList queryMail = new QueryMarketingItemList("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData(arr));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
